package com.see.viewer.data;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.transaction.Transaction;

import com.see.domain.dao.DataNucleusUtil;
import com.see.domain.dao.PMF;
import com.see.domain.model.StaticData;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public final class StaticDataAdaptor {

	public static final Object TITLE_SYMBOL = "symbol";
	public static final Object TITLE_SECTOR = "sector";
	public static final Object TITLE_INDUSTRY = "industry";
	private static DataNucleusUtil util = new DataNucleusUtil();
	private static List<StaticData> listOfRecords = new ArrayList();
	
	
	public void insertAllData() {
		StaticData staticDataTemp = new StaticData();
		staticDataTemp.setSymbol("aaaa");
		staticDataTemp.setIndustry("sdfad");
		staticDataTemp.setSector("dfasae");
		
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		javax.jdo.Transaction tx=persistenceManager.currentTransaction();
		try {
			tx.begin();
			persistenceManager.makePersistent(staticDataTemp);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			 if (tx.isActive())
			    {
			        tx.rollback();
			    }

			 persistenceManager.close();
		}
		System.out.println("insert success");
	}

	public  IndexedContainer getContainer() {
		return fillStaticDataContainer();
	}

	private  IndexedContainer fillStaticDataContainer() {
		IndexedContainer container = new IndexedContainer();
		container.addContainerProperty(TITLE_SYMBOL, String.class, null);
		container.addContainerProperty(TITLE_SECTOR, String.class, null);
		container.addContainerProperty(TITLE_INDUSTRY, String.class, null);

		// get full list by default
		listOfRecords = util.getFullListOfRecords();

		/*StaticData staticDataTemp = new StaticData();
		staticDataTemp.setSymbol("aaaa");
		staticDataTemp.setIndustry("sdfad");
		staticDataTemp.setSector("dfasae");
		listOfRecords.add(staticDataTemp);*/
		if (listOfRecords!= null && listOfRecords.size() > 0) {
			for (StaticData staticData : listOfRecords) {
				Item item = container.addItem(staticData.getSymbol());
				item.getItemProperty(TITLE_SYMBOL).setValue(
						staticData.getSymbol());
				item.getItemProperty(TITLE_SECTOR).setValue(
						staticData.getSector());
				item.getItemProperty(TITLE_INDUSTRY).setValue(
						staticData.getIndustry());
			}
		}
		container.sort(new Object[] { TITLE_SYMBOL }, new boolean[] { true });
		return  container;
	}

}